package Gui;

import java.awt.GridLayout;
import java.util.ArrayList;

import javax.swing.JLabel;
import javax.swing.JPanel;

import Main.Defines;
import PlayerManager.Player;

public class BottomStatsPanel extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = -9048819386180852244L;
	private ArrayList<JLabel> m_playersCashLoanList;
	private JLabel m_jackpot;
	
	// loan amount
	private static BottomStatsPanel ref;

	private void initLabels() {
		m_playersCashLoanList = new ArrayList<JLabel>();
		for (Player a : Defines.PLM.getPlayerList()) {
			JLabel thisPlayersLabel = new JLabel();
			String labelText = "";
			labelText += "<html>Player " + a.getIDPrintable();
			labelText += "<br> Cash: $" + a.getCash();
			labelText += "<br> Loan: $" + a.getLoan();
			thisPlayersLabel.setText(labelText);
			m_playersCashLoanList.add(thisPlayersLabel);
		}
		for (JLabel a : m_playersCashLoanList)
			add(a);
	}

	private BottomStatsPanel() {
		initLabels();

		m_jackpot = new JLabel("<html>Jackpot: $" + Defines.GM.getLotteryPile() + "<br>Bonus: $" + Defines.GM.getBonusPile());
		
		add(m_jackpot);
		setLayout(new GridLayout(Defines.SINGLE_ROW_COLUMN_MODIFIER,
				m_playersCashLoanList.size()));

	}

	public void resetInstance() {
		for (JLabel a : m_playersCashLoanList)
			remove(a);
		initLabels();
		m_jackpot.setText("<html>Jackpot: $" + Defines.GM.getLotteryPile() + "<br>Bonus: $" + Defines.GM.getBonusPile());
	}

	public static BottomStatsPanel getInstance() {
		if (ref == null) {
			ref = new BottomStatsPanel();
		}
		return ref;
	}

	public void update() {

		for (int i = 0; i < m_playersCashLoanList.size(); ++i) {
			Player a = Defines.PLM.getPlayerList().get(i);

			String labelText = "";
			labelText += "<html>Player " + a.getIDPrintable();
			labelText += "<br> Cash: $" + a.getCash();
			labelText += "<br> Loan: $" + a.getLoan();
			m_playersCashLoanList.get(i).setText(labelText);
		}
		m_jackpot.setText("<html>Jackpot: $" + Defines.GM.getLotteryPile() + "<br>Bonus: $" + Defines.GM.getBonusPile());
		

	}

}
